import { getDismiss, confrimDismiss } from "../../../../api/manage"
import create from 'dd-store'
import pageStore from '../../../../stores/globalStore'


create.Page({
  store: pageStore,
  data: {
    // 路由数据
    routeData: {},
    // 驳回流程列表
    rebutList: [],
    // 当前登录用户id
    userId: '',
    // 驳回原因
    rebutCause: '',
    // 当前选择的驳回流程id
    currentRebutProgressId: 0
  },
  onLoad(options) {
    console.log('页面接收数据', options);
    this.setData({
      routeData: options
    })
    this.getRebutListFunc()
  },
  async getRebutListFunc() {
    await dd.getStorage({
      key: 'currentCity',
      success: (res) => {
        let data = {
          userDDId: res.data.value.userId,
          nowPmProgressId: this.data.routeData.nowPmProgressId
        }
        console.log('驳回流程查询参数', data);
        getDismiss(data).then(rs => {
          let list = rs.data.filter((rl) => {
            return rl.pmStageCode != '0036'
          })
          console.log('驳回流程列表', list);
          list.forEach((ls) => {
            ls.text = ls.makeUserName + '--' + ls.pmProgressName
            ls.value = ls.pmProgressId
          })
          console.log('驳回流程整理数据', list);
          this.setData({
            rebutList: list,
            userId: res.data.value.userId
          })
        })
      },
      fail: function (res) {
        dd.alert({ content: '请稍后再试' });
      }
    })
  },
  // 驳回流程选择Change事件
  rebutProgressChange(e) {
    console.log('当前选择流程', e);
    this.setData({
      currentRebutProgressId: e
    })
  },
  // 驳回原因文本域Change事件
  rebutCauseTextareaChange(e) {
    console.log(e);
    this.setData({
      rebutCause: e
    })
  },
  // 确认驳回点击事件
  async confirmRebutClick() {
    if (!this.data.currentRebutProgressId) {
      dd.showToast({
        content: "未选择驳回流程",
        type: "fail",
        duration: 1500
      })
      return
    }
    let data = {
      makeUserDDId: this.data.userId,
      nowPmProgressId: Number(this.data.routeData.nowPmProgressId),
      rebutPmProgressId: this.data.currentRebutProgressId,
      rebutCause: this.data.rebutCause
    }
    console.log('驳回提交数据', data);
    let rebutProgressName = ''
    this.data.rebutList.forEach((item) => {
      if (item.pmProgressId == this.data.currentRebutProgressId) {
        rebutProgressName = `${item.makeUserName}--${item.pmProgressName}`
      }
    })

    dd.confirm({
      title: '温馨提示',
      content: `您是否要驳回该流程到 ${rebutProgressName}`,
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm) {
          dd.showLoading({
            content: "驳回中"
          })
          confrimDismiss(data).then((res) => {
            if (res.result == "success") {
              dd.showToast({
                content: "驳回成功",
                type: "success",
                duration: 1000
              })
              setTimeout(() => {
                dd.navigateBack({
                  delta: 1
                })
              }, 1000);
            }
          })
        }

      },
    });

  },
});
